'use strict';
module.exports = {
	/**
	 * XXXnameXXX
	 * @url client/pages/product/kh/get_product_info 前端调用的url参数地址
	 * data 请求参数
	 * @param {String} params1  参数1
	 */
	main: async (event) => {
		let {
			data = {}, userInfo, util, filterResponse, originalParam
		} = event;
		let {
			customUtil,
			uniID,
			config,
			pubFun,
			vk,
			db,
			_
		} = util;
		let {
			uid,
			name,
			sortArr,
			search_title
		} = data;
		let res = {
			code: 0,
			msg: ""
		};
		// 业务逻辑开始-----------------------------------------------------------
		console.log('sortArr', sortArr)
		console.log('name', name)
		if (name === 'all') name = '_id'

		let regExp = new RegExp(search_title);


		let whereJson = {};
		let andArr = [];
		if (search_title) {
			// 查询包含search_title的数据
			try {
				let regExp = new RegExp(search_title);
				let orObj = _.and([{
						"title": regExp
					},
					{
						status: 1
					}
				]);
				andArr.push(orObj);
			} catch (err) {
				return {
					code: -1,
					msg: '请输入合法的查询内容'
				};
			}
		}

		if (andArr.length > 0) {
			whereJson = _.and(andArr);
		} else {
			whereJson = {
				status: 1
			}
		}


		console.log('whereJson', whereJson)
		if (sortArr !== 'all') {
			console.log("我进来了")
			res.data = await vk.baseDao.select({
				pageSize:-1,
				getMain: true,
				dbName: "daoju_merchandise",
				whereJson,
				sortArr: [{
					name: name,
					type: sortArr
				}],
			});
		}


		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}